package com.zondy.mapgis.psmap.manager.controller;

import com.zondy.mapgis.common.controllerprefix.annotation.ManagerRestController;
import com.zondy.mapgis.common.core.web.domain.AjaxResult;
import com.zondy.mapgis.common.log.annotation.Log;
import com.zondy.mapgis.common.log.enums.BusinessType;
import com.zondy.mapgis.common.security.annotation.RequiresPermissions;
import com.zondy.mapgis.domain.psmap.AppPermission;
import com.zondy.mapgis.psmap.manager.service.IAppPermissionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

@Tag(
   name = "应用权限管理",
   description = "应用权限控制器"
)
@ManagerRestController({"/app/permission"})
public class AppPermissionController {
   private final IAppPermissionService appPermissionService;

   @Operation(
      summary = "加载对应角色数据列表树"
   )
   @PreAuthorize("@ss.hasPermi('app:permission:list')")
   @RequiresPermissions({"app:permission:list"})
   @GetMapping({"/roleDataTreeSelect/{roleId}"})
   public AjaxResult roleDataTreeSelect(@PathVariable("roleId") Long roleId) {
      return this.appPermissionService.roleDataTreeSelect(roleId);
   }

   @Operation(
      summary = "修改保存数据权限"
   )
   @PreAuthorize("@ss.hasPermi('app:permission:edit')")
   @RequiresPermissions({"app:permission:edit"})
   @Log(
      title = "应用权限管理",
      businessType = BusinessType.UPDATE
   )
   @PutMapping({"/data"})
   public AjaxResult editData(@RequestBody AppPermission appPermission) {
      return this.appPermissionService.updateDataPermission(appPermission) > 0 ? AjaxResult.success() : AjaxResult.error("修改角色应用权限失败，请联系管理员");
   }

   @Operation(
      summary = "获取微件权限"
   )
   @PreAuthorize("@ss.hasPermi('app:permission:list')")
   @RequiresPermissions({"app:permission:list"})
   @Log(
      title = "应用权限管理",
      businessType = BusinessType.UPDATE
   )
   @GetMapping({"/widget"})
   public AjaxResult getWidget(@RequestParam Long roleId) {
      return AjaxResult.success(this.appPermissionService.selectStoreWidgetByRoleId(roleId));
   }

   @Operation(
      summary = "修改保存微件权限"
   )
   @PreAuthorize("@ss.hasPermi('app:permission:edit')")
   @RequiresPermissions({"app:permission:edit"})
   @Log(
      title = "应用权限管理",
      businessType = BusinessType.UPDATE
   )
   @PutMapping({"/widget"})
   public AjaxResult eidtWidget(@RequestBody AppPermission appPermission) {
      return AjaxResult.success(this.appPermissionService.updateWidgetPermission(appPermission));
   }

   @Autowired
   public AppPermissionController(IAppPermissionService appPermissionService) {
      this.appPermissionService = appPermissionService;
   }
}
